using System;
using System.Windows.Controls;
using System.Collections.Generic;
using Silvery.Data;
namespace Silvery
{   
    public class NSpiritData : Dictionary<string, List<Image>>,ISpiritData
    {        
        public bool IsDisposeClear { get; set; }
        public NSpiritData()
        {
            IsDisposeClear = true;
        }
        /// <summary>
        /// 从控件创建，老方法，但是没有数据
        /// </summary>
        /// <param name="data"></param>
        public static ISpiritData Create(UserControl data)
        {
            NSpiritData spiritdata = new NSpiritData();
            //考虑从控件来的创建可能会多处使用
            spiritdata.IsDisposeClear = false;
            var d = data.Content as Panel;
            if (d !=null)
            {
                foreach (var item in d.Children)
                {
                    var t = item as Panel;
                    if (t != null)
                    {
                        foreach (var img in t.Children)
                        {
                            var frame = img as Image;
                            if (frame != null)
                            {
                                spiritdata.AddFrame(t.Name, frame);
                            }
                        }
                    }
                }
            }
            return spiritdata;
        }
        
        public void AddFrame(string aniname, Image frame)
        {
            if (!ContainsKey(aniname))
                Add(aniname,new List<Image>());
            this[aniname].Add(frame);
        }
        private List<Image> GetFramesFromAniname(string aniname)
        {
            if (ContainsKey(aniname))
                return this[aniname];
            else
                return null;
                //throw new NotImplementedException("访问了不存在的精灵数据:" + aniname);
        }
        public IList<Image> FindAnimat(string aniname)
        {
            return GetFramesFromAniname(aniname);          
        }
        /// <summary>
        /// 销毁
        /// </summary>
        public void Dispose()
        {
            if (IsDisposeClear)
            {
                foreach (var item in this.Values)
                {
                    foreach (var t in item)
                    {
                        t.Source = null;
                    }
                    item.Clear();
                }
            }
            else
                foreach (var item in this.Values)
                    item.Clear();
            this.Clear();
            animatNamesList.Clear();
            animatNamesList = null;
            FramesInfo = null;
        }
        public SpiritFile_Frames FramesInfo { get; internal set; }

        List<string> animatNamesList = new List<string>();
        /// <summary>
        /// 所有动画名字的列表
        /// </summary>
        public List<string> AnimatNamesList
        {
            get
            {
                if (animatNamesList.Count != this.Keys.Count)
                {
                    animatNamesList.Clear();
                    foreach (var item in this.Keys)
                        animatNamesList.Add(item);  
                }
                return animatNamesList;
            }
        }
   }
    
}
